using System;
using System.Drawing;
using System.Reflection;
using System.Resources;

namespace Wantn.Share
{
	public sealed class ResOpt
	{
		public static Image GetImage(Assembly asmApp, string resRootName, string resName)
		{
			try
			{
				ResourceManager resourceManager = new ResourceManager(resRootName, asmApp);
				object @object = resourceManager.GetObject(resName);
				if (@object == null)
				{
					return null;
				}
				if (@object.GetType().Name == "Icon")
				{
					return ((Icon)@object).ToBitmap();
				}
				return (Image)@object;
			}
			catch
			{
				return null;
			}
		}

		public static Image GetImage(Assembly asmApp, string resRootName, string resName, int width, int height)
		{
			Image image = GetImage(asmApp, resRootName, resName);
			if (image != null)
			{
				image = image.GetThumbnailImage(width, height, null, IntPtr.Zero);
			}
			return image;
		}

		public static Icon GetIcon(Assembly asmApp, string resRootName, string resName)
		{
			Icon result = null;
			try
			{
				Image image = GetImage(asmApp, resRootName, resName);
				if (image == null)
				{
					return result;
				}
				result = Icon.FromHandle(((Bitmap)image).GetHicon());
				return result;
			}
			catch
			{
				return result;
			}
		}

		public static Icon GetIcon(Assembly asmApp, string resRootName, string resName, int width, int height)
		{
			Icon result = null;
			try
			{
				Image image = GetImage(asmApp, resRootName, resName, width, height);
				if (image == null)
				{
					return result;
				}
				result = Icon.FromHandle(((Bitmap)image).GetHicon());
				return result;
			}
			catch
			{
				return result;
			}
		}

	}
}
